{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "1.1 利用map批量检测用户名是否符合规范=> username符合规范username不符合规范\n",
    "- 只能包含数据、字母、下划线,不以数字开头，长度在6-18之间\n",
    "\n",
    "1.2 filter把合法的用户名显示出来"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'list' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-29-50a028b346b0>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m     13\u001B[0m \u001B[0musername\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;33m[\u001B[0m\u001B[1;34m'asdfg12345'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;34m'asdfghjkl1234567890000000'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;34m'asd'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;34m'123456789'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;34m'asdf12345sd'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     14\u001B[0m \u001B[1;31m#1.1\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 15\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlist\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mmap\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstd_or_not\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0musername\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     16\u001B[0m \u001B[1;31m#1.2\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     17\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlist\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mfilter\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstd_or_not\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0musername\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: 'list' object is not callable"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import string\n",
    "\n",
    "def std_or_not(str):\n",
    "    lst = list(str)\n",
    "    if lst[0].isdigit() == False:\n",
    "        if 6 <= len(lst) <= 18:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "username = ['asdfg12345','asdfghjkl1234567890000000','asd','123456789','asdf12345sd']\n",
    "#1.1\n",
    "print(list(map(std_or_not,username)))\n",
    "#1.2\n",
    "print(list(filter(std_or_not,username)))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "2.匿名函数+ reduce实现1*2*3*..*n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "362880\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "n=10\n",
    "# print(list(map(lambda item:item*item,[i for i in range(n)])))\n",
    "print(reduce(lambda x,y:x*y,[i for i in range(1,n)]))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "3.1 filter函数:找出[1-100]能被3整除的数\n",
    "\n",
    "3.2 map函数:求list[1-10]各项的平方"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "#3.1\n",
    "print(list(filter(lambda item:item%3 == 0,[i for i in range(1,101)])))\n",
    "#3.2\n",
    "print(list(map(lambda item:item*item,[i for i in range(1,11)])))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "4.将列表[1,4,5,9]转换成'1459'的字符串"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1459 \n",
      "Type is <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "lst = [1,4,5,9]\n",
    "def custom_str(lst):\n",
    "    str = ''\n",
    "    def int_to_char(lst):\n",
    "        all_number_dict = {0:'0', 1:'1', 2:'2', 3: '3', 4: '4',5:'5',6:'6',7:'7',8:'8',9:'9'}\n",
    "        return all_number_dict[lst]\n",
    "    try:\n",
    "        for i in range(len(lst)):\n",
    "            lst[i] = int_to_char(lst[i])\n",
    "            # print(type(lst[i]),type(str))\n",
    "            str = str+lst[i]\n",
    "            # print(str)\n",
    "    except:\n",
    "        return str\n",
    "    return str\n",
    "str = custom_str(lst)\n",
    "print(str,'\\nType is',type(str))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "5.有以下列表: list1=[7,-8,5,4,0,-2, -5]\n",
    "- 正数在前，负数在后\n",
    "- 整数从小到大\n",
    "- 负数从大到小"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 7, 0, -2, -5, -8]\n"
     ]
    }
   ],
   "source": [
    "list1=[7,-8,5,4,0,-2, -5]\n",
    "positive = []\n",
    "negative = []\n",
    "zero = []\n",
    "for i in range(len(list1)):\n",
    "    if list1[i] > 0:\n",
    "        positive.append(list1[i])\n",
    "    elif list1[i] < 0:\n",
    "        negative.append(list1[i])\n",
    "    else:\n",
    "        zero.append(list1[i])\n",
    "positive = sorted(positive)\n",
    "negative = sorted(negative,reverse=True)\n",
    "\n",
    "positive.extend(zero)\n",
    "positive.extend(negative)\n",
    "new_one = positive\n",
    "print(new_one)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "6.这是一个字符串排序,排序规则:小写<大写<奇数<偶数\n",
    "- S = 'asdf234GDSdsf23' #排序:小写-大写-奇数-偶数\n",
    "- 原理:先比较元组的第一个值, FALSE"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "S = 'asdf234GDSdsf23'\n",
    "lower = []\n",
    "upper = []\n",
    "odd = []\n",
    "even = []"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "addffssDGS33224\n"
     ]
    }
   ],
   "source": [
    "s = 'asdf234GDSdsf23'\n",
    "\n",
    "def str_sort(s):\n",
    "    for temp in s:\n",
    "        if temp.isdigit():\n",
    "            if int(temp) % 2 == 0:\n",
    "                return (1, 1, temp)\n",
    "            else:\n",
    "                return (1, 0, temp)\n",
    "        else:\n",
    "            if temp.isupper():\n",
    "                return (0, 1, temp)\n",
    "            else:\n",
    "                return (0, 0, temp)\n",
    "string = ''.join(sorted(s, key=str_sort))\n",
    "print(string)\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}